import axios from 'axios'

// 导航模式类型
export type NavigationMode = 'driving' | 'walking' | 'bicycling' | 'transit'

// 高德地图驾车路径规划响应类型
export interface GaoDeRouteResponse {
  status: string
  info: string
  infocode: string
  count: string
  route: {
    origin: string
    destination: string
    paths: Path[]
  }
}

// 高德地图步行路径规划响应类型
export interface GaoDeWalkingRouteResponse {
  status: string
  info: string
  infocode: string
  count: string
  route: {
    origin: string
    destination: string
    paths: WalkingPath[]
  }
}

// 高德地图骑行路径规划响应类型
export interface GaoDeBicyclingRouteResponse {
  data: {
    origin: string
    destination: string
    paths: BicyclingPath[]
  }
  errcode: number
  errdetail: string | null
  errmsg: string
}

// 高德地图公交路径规划响应类型
export interface GaoDeTransitRouteResponse {
  status: string
  info: string
  infocode: string
  count: string
  route: {
    origin: string
    destination: string
    distance: string
    taxi_cost: string
    transits: Transit[]
  }
}

// 驾车路径
interface Path {
  distance: string
  duration: string
  strategy: string
  tolls: string
  toll_distance: string
  steps: Step[]
  restriction: string
  traffic_lights: string
}

// 步行路径
interface WalkingPath {
  distance: string
  duration: string
  steps: WalkingStep[]
}

// 骑行路径
interface BicyclingPath {
  distance: number
  duration: number
  steps: BicyclingStep[]
}

// 公交换乘方案
interface Transit {
  cost: string
  duration: string
  walking_distance: string
  distance: string
  segments: TransitSegment[]
}

// 公交换乘路段
interface TransitSegment {
  walking: {
    origin: string
    destination: string
    distance: string
    duration: string
    steps: WalkingStep[]
  }
  bus?: {
    buslines: BusLine[]
  }
  entrance?: {
    name: string
    location: string
  }
  exit?: {
    name: string
    location: string
  }
}

// 公交线路
interface BusLine {
  departure_stop: {
    name: string
    id: string
    location: string
  }
  arrival_stop: {
    name: string
    id: string
    location: string
  }
  name: string
  id: string
  type: string
  distance: string
  duration: string
  polyline: string
  via_num: string
  via_stops: {
    name: string
    id: string
    location: string
  }[]
}

// 驾车路段
export interface Step {
  instruction: string
  orientation: string
  distance: string
  tolls: string
  toll_distance: string
  toll_road: never[] // 可根据实际数据结构进一步细化
  duration: string
  polyline: string
  action: string
  assistant_action: string
}

// 步行路段
export interface WalkingStep {
  instruction: string
  orientation: string
  road: string
  distance: string
  duration: string
  polyline: string
  action: string
  assistant_action: string
  walk_type?: string
}

// 骑行路段
export interface BicyclingStep {
  instruction: string
  orientation: string
  road: string
  distance: number
  duration: number
  polyline: string
  action: string
  assistant_action: string
  walk_type?: number
}

// 添加交通状态响应类型
export interface GaoDeTrafficResponse {
  status: string
  info: string
  infocode: string
  trafficinfo: {
    description: string
    evaluation: {
      expedite: string
      congested: string
      blocked: string
      status: string
      description: string
    }
  }
}

// 高德地图API密钥
const GAODE_API_KEY = '7f7a58932a276710392d574317aacdf4'

export async function getGaoDeTraffic(
  roadName: string,
  adcode: string,
): Promise<GaoDeTrafficResponse> {
  const result = await axios.get(
    `https://restapi.amap.com/v3/traffic/status/road?name=${roadName}&adcode=${adcode}&key=${GAODE_API_KEY}`,
  )
  return result.data
}

// 获取驾车路线
export async function getGaoDeDrivingRoute(
  start: string,
  end: string,
): Promise<GaoDeRouteResponse> {
  const result = await axios.get(
    `https://restapi.amap.com/v3/direction/driving?origin=${start}&destination=${end}&extensions=all&output=json&key=${GAODE_API_KEY}`,
  )
  return result.data
}

// 获取步行路线
export async function getGaoDeWalkingRoute(
  start: string,
  end: string,
): Promise<GaoDeWalkingRouteResponse> {
  const result = await axios.get(
    `https://restapi.amap.com/v3/direction/walking?origin=${start}&destination=${end}&output=json&key=${GAODE_API_KEY}`,
  )
  return result.data
}

// 获取骑行路线
export async function getGaoDeBicyclingRoute(
  start: string,
  end: string,
): Promise<GaoDeBicyclingRouteResponse> {
  const result = await axios.get(
    `https://restapi.amap.com/v4/direction/bicycling?origin=${start}&destination=${end}&key=${GAODE_API_KEY}`,
  )
  return result.data
}

// 获取公交路线
export async function getGaoDeTransitRoute(start: string, end: string, city: string = '北京'): Promise<GaoDeTransitRouteResponse> {
  const result = await axios.get(
    `https://restapi.amap.com/v3/direction/transit/integrated?origin=${start}&destination=${end}&city=${city}&output=json&key=${GAODE_API_KEY}`,
  )
  return result.data
}

// 统一的路线获取函数，根据模式选择不同的API
export async function getGaoDeRoute(start: string, end: string, mode: NavigationMode = 'driving'): Promise<any> {
  switch (mode) {
    case 'walking':
      return getGaoDeWalkingRoute(start, end)
    case 'bicycling':
      return getGaoDeBicyclingRoute(start, end)
    case 'transit':
      return getGaoDeTransitRoute(start, end)
    case 'driving':
    default:
      return getGaoDeDrivingRoute(start, end)
  }
}
